(function(wi) {
    function log(msg) {
        console.log("jpush:" + msg)
    }
    var timer = setInterval(function() {
        if (api) {
            if (timer) {
                clearInterval(timer);
            }
            registerJpush(api);
        }
    }, 1000);

    function registerJpush(api) {
        jpush = api.require("ajpush");

        if (api.systemType == "android") {
            jpush.init(function(ret) {
                log(JSON.stringify(ret));
                log(jpushListener)
                jpush.setListener(jpushListener);
            });
        } else if (api.systemType == "ios") {
            jpush.setListener(jpushListener);
        }
        api.addEventListener({
            name: 'appintent'
        }, function(ret, err) {
            // alert(JSON.stringify(ret));
            jpushListener(ret.appParam.ajpush)
            // alert(JSON.stringify(ret));
        })
        // api.addEventListener({
        //     name: 'pause'
        // }, function(ret, err) {
        //     onPause(); //监听应用进入后台，通知jpush暂停事件
        // })
        // api.addEventListener({
        //     name: 'resume'
        // }, function(ret, err) {
        //     onResume(); //监听应用恢复到前台，通知jpush恢复事件
        // })

        function jpushListener(ret) {
          var data={
            win_name: ret.extra.gid+'goods_detail_win'+new Date().getTime(),
            item_id:ret.extra.gid
          }
          log(JSON.stringify(data));
          open_goods(data);
        }
        //统计-app恢复
        function onResume() {
            jpush.onResume();
            log('JPush onResume');
        }

        //统计-app暂停
        function onPause() {
            jpush.onPause();
            log('JPush onPause');
        }

    };


})(window)
